package top.hudk.tank.strategy;


import top.hudk.tank.constant.GameConstant;
import top.hudk.tank.model.GameApplicationContext;
import top.hudk.tank.utils.Audio;
import top.hudk.tank.enums.Group;
import top.hudk.tank.factory.Bullet;
import top.hudk.tank.factory.Tank;
import top.hudk.tank.utils.ThreadPoolUtils;

/**
 * 作用: 单发开火策略
 *
 * @author hudk
 * @date 2020/9/9 21:10
 */
public class SingleShotFireStrategy implements FireStrategy{

    private static SingleShotFireStrategy instance = null;

    private SingleShotFireStrategy(){
        if(instance != null){
            throw new RuntimeException("本对象是单例模式，系统内不允许多出现个实例");
        }
    }

    /**
     * 单例
     * @return
     */
    public static SingleShotFireStrategy getInstance(){
        if(instance == null){
            synchronized(SingleShotFireStrategy.class){
                if(instance == null){
                    instance = new SingleShotFireStrategy();
                    return instance;
                }
            }
        }
        return instance;
    }


    @Override
    public void fire(Tank tank) {
        //将子弹加载到画面中
        Bullet bullet = GameApplicationContext.getInstance().getFactory().createBullet();
        //子弹初始坐标位置
        int x = tank.getX()+tank.getWidth()/2- bullet.getWidth()/2;
        int y = tank.getY()+tank.getHeight()/2-bullet.getHeight()/2;
        bullet.init(x,y,tank.getDir(),tank.getGroup());
        if(tank.getGroup() == Group.GOOD) {
            ThreadPoolUtils.execute(()->new Audio(GameConstant.EXPLODE_MUSIC_FILE_PATH).play());
        }
    }
}
